
#ifndef _BASE_SERVER_
#define _BASE_SERVER_
// defines base server

#include "ClientCount.h"
#include "RMSListenSocket.h"
#include "RMSClientID.h"
#include "..\RMSTranslator\Camera.h"
#include "..\RMSLib\info.h"

#define MAX_CAMERAS 256

typedef map<CString, CSite> MAPINFO;

class CRecordDlg;

class CBaseServer {
	friend CRMSListenSocket;
public:
	CBaseServer();
	~CBaseServer();

	list<LPRMSClientSocket> m_ListOfClients;
	list<LPRMSWaveSocket>	m_ListOfWaveSocket;

	void					AllocateData			(int iMaxCameras);
	void					Close					();
	int						GetMaxCameras			();

	void					StartCritical			();
	void					EndCritical				();

	BOOL					TerminateWaveSesson		(BYTE byID);

	void					SetOutListBox			(CListBox*	pListBoxCliets = NULL, CListBox*	pListBoxCameras = NULL);
	void					UpdateListBox			();

	void					ListenInit(const char* szIP, const char* szPort);
	BOOL					ListenStart();
	void					ListenStop();
#ifdef _CYCLIC_BUFFER_
	void					InitStorage();
#endif
	BOOL					AddWaveSocket			(CRMSWaveSocket* pWaveSocket);
	BOOL					SendWaveEvent			(int nCameraID = 0);
	BOOL					SendVideoEvent			(int nCameraID = 0);

	int*					GetDataPtr_SizeDate		(int CameraID);
	int*					GetDataPtr_SizeTime		(int CameraID);
	int*					GetDataPtr_SizeVideo	(int CameraID);
	int*					GetDataPtr_SizeAudio	(int CameraID);

	BYTE*					GetDataPtr_dtDate		(int CameraID);
	BYTE*					GetDataPtr_dtTime		(int CameraID);
	BYTE*					GetDataPtr_dtVideo		(int CameraID);
	BYTE*					GetDataPtr_dtAudio		(int CameraID);
	DWORD*					GetDataPtr_TypeVideo	(int CameraID);

	
	CRITICAL_SECTION*		GetDataPtr_CriticalData(int CameraID);
	CRITICAL_SECTION*		GetDataPtr_CriticalAudio(int CameraID);
	
	int						CheckStartCamera		(int CameraID, BOOL bEnableNoVideo = FALSE,
													 HANDLE h1 = NULL, HANDLE h2 = NULL,
													 BOOL bEnableRing = FALSE, char* pCaller = NULL);
	void					SetCamPassword			(int CameraID,char* pass);


	void					AddCamera				(const char *szInfo);
	void					DeleteCamera			(const char *szInfo);

	int 					AddClient				(const char *szInfo);
	void					DeleteClient			(const char *szInfo,int ID);
	void					acceptClient			(long cid, short status);
	CString					formatClientTitle		(LPCTSTR pip, LPCTSTR pcs);
	CString					getClientTitle			(long cid);
	CString					getAllClientTitles		();
	DWORD					GetClientCount			();

	BOOL					SetReflectorIP			(const char* szIP);
	char*					GetReflectorIP			();
	CString					GetProperIPForCamera	(const char* camIP);

	void					DeletePassiveClients	();
	void					DeletePassiveCameras	();
#ifdef _CYCLIC_BUFFER_
	void					DeleteOldFiles			();
	void					CheckDeleteOldFiles		();
	void					SignalDeleteOldFiles	();
#endif

	void					LoadInfo				();
	void					UpdateInfo				(MAPINFO& mapInfo);
	BOOL					GetCameraInfo			(int nCameraID, CInfo& info);
	const CInfo*			getCameraInfoObject		(int CameraID);
	DWORD					checkReturnedAddress	(int, DWORD, DWORD);
	BOOL					isInternal				(DWORD dwCam, DWORD dwInt, DWORD dwExt);
	DWORD					getSelectedAddr			();
	void					setSelectedAddr			(const char* szIP);
	void					TerminateCameras		(int CameraID);

	MAPINFO					GetMapInfo				();

	void					LoadPassword			();
	void					SavePassword			();
	void					SetPassword				(const char* szPassword);
	int						GetPasswordLen			();
	BOOL					CheckPassword			(const char* szPassword);

	BOOL					GetRecordState			();
	void					SetRecordState			(BOOL bRecordState);
#ifdef _CYCLIC_BUFFER_
	int						GetRecordCycle			();
	void					SetRecordCycle			(int nCycle);

	BOOL					GetServerTime			(CTime& time);
	BOOL					DateIsChanged			(BOOL bStatus = FALSE);
#endif

	BOOL					OpenRecordDlg			(CWnd* pWnd);
	BOOL					CloseRecordDlg			();
	int						SengRing				(int CameraID,DWORD* ring);
	int						BasePort;				// base port,777 by default in "n"

	CString					GetClientAddress		(LPCTSTR szIP);

	void					ClientEnableNoVideo		(BYTE clientID, BOOL bEnable);

	// recording video
	CRecordDlg*				m_pRecordDlg;
	BOOL					m_bRecord;
	void					SaveCameraAudio			(int CamID, BYTE n);

#ifdef _IPR_
	// valiables for Encoder runs with -ipr option
	BOOL					m_bEnableIpr;
	CRITICAL_SECTION		m_IprCritical;
	map <CString, CAllowIP>  m_registIp;
#endif //def _IPR_

private:
	// Cameras info
	MAPINFO					m_mapInfo;
	CRITICAL_SECTION		m_MapInfoCritical;

	// password
	char					m_szPassword[128];
	CRITICAL_SECTION		m_PasswordCritical;

	char					m_szReflectorIP[255];

	CClientCount			m_ClientCount;
	CRMSClientID			m_ClientID;

	int						m_sourceID[MAX_CAMERAS];

	CLiveCamera				*m_pLiveCamera[MAX_CAMERAS];
	CRMSListenSocket*		m_pListenSocket;
	CRMSListenSocket*		m_pListenSocketInternal;
	
	CRITICAL_SECTION		m_ClientCritical,m_CameraCritical;

	// data for audio/video exchange
	int						m_nMaxCameras;
	
	int						*m_pSzDate[MAX_CAMERAS];
	int						*m_pSzTime[MAX_CAMERAS];
	int						*m_pSzVideo[MAX_CAMERAS];
	int						*m_pSzAudio[MAX_CAMERAS];
    DWORD                   *m_dwTypeVideo[MAX_CAMERAS];

	BYTE					*m_dtDate[MAX_CAMERAS];
	BYTE					*m_dtTime[MAX_CAMERAS];
	BYTE					*m_dtVideo[MAX_CAMERAS];
	BYTE					*m_dtAudio[MAX_CAMERAS];
	
	CRITICAL_SECTION		*m_pCriticalData[MAX_CAMERAS];
	CRITICAL_SECTION		*m_pCriticalAudio[MAX_CAMERAS];

	// List of clients & cameras
	BOOL					m_bNeedUpdateList;

	CListBox*				m_pListClients;
	CListBox*				m_pListCameras;
	list<CString>			m_listStrCameras;
	list<CString>			m_listStrClients;
	
	CRITICAL_SECTION		m_ClientListCritical,m_CameraListCritical;

#ifdef _CYCLIC_BUFFER_
	BOOL					m_bDateIsChanged;
	BOOL					m_bDeleteAllOldFiles;
	int						m_nCycle;
	CTime					m_timeCurrentTime;
#endif
	CRITICAL_SECTION		m_RecordCritical;

	int						GetCamIndex				(int CameraID);

	DWORD					m_selectedAddr;


public:
	void DeleteAllClients();
	void SetMode_NewPassword(int CamID);
	void GetPassword(char * pszPass);
};


#endif
